package com.wiscamp.ninechapters.problems.adapter.resources.collections;


import com.wiscamp.ninechapters.problems.application.dto.commands.collections.CreateCollectionPartCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.CreateCollectionSectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.UpdateCollectionPartCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.collections.UpdateCollectionSectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.collections.CollectionPartVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.collections.ColllectionSectionVO;
import com.wiscamp.ninechapters.problems.application.services.CollectionPartAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 题集试卷服务
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")

@Tag(name = "题库-题集试卷信息")
public class CollectionPartController extends BaseController {
    private final CollectionPartAppService CollectionPartAppService;

    public CollectionPartController(CollectionPartAppService problemCollectionAppService) {
        this.CollectionPartAppService = problemCollectionAppService;
    }

    /**
     * 通过题集编号获取题集试卷部分列表
     *
     * @return 题集部分列表
     */
    @GetMapping("ProblemCollections/{collectionId}/CollectionParts")
    @Operation(summary = "通过题集编号获取题集试卷部分列表")
    @HandleResourceException
    public RestResponse<Collection<CollectionPartVO>> getCollectionParts(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                         @PathVariable("collectionId") long collectionId) {
        return ResourceUtils.getListById(request, response, collectionId, CollectionPartAppService::getCollectionParts);
    }

    /**
     * 创建题集试卷部分
     *
     * @param command
     * @return
     */
    @PostMapping("CollectionParts")
    @Operation(summary = "创建题集试卷部分")
    @HandleResourceException
    public RestResponse<CollectionPartVO> createCollectionPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                               @RequestBody CreateCollectionPartCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, CollectionPartAppService::createCollectionPart);
    }

    /**
     * 更新题集试卷部分
     *
     * @param command
     * @return
     */
    @PutMapping("CollectionParts")
    @Operation(summary = "更新题集试卷部分")
    @HandleResourceException
    public RestResponse<CollectionPartVO> updateCollectionPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                               @RequestBody UpdateCollectionPartCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, CollectionPartAppService::updateCollectionPart);
    }

    /**
     * 通过部分编号删除题集试卷部分
     *
     * @param partId
     * @return
     */
    @DeleteMapping("CollectionParts")
    @Operation(summary = "通过部分编号删除题集试卷部分")
    @HandleResourceException
    public RestResponse<Boolean> deleteCollectionPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                      @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, CollectionPartAppService::deleteCollectionPart);
    }


    /**
     * 通过试卷部分编号获取题集试卷节列表
     *
     * @return 题集节列表
     */
    @GetMapping("CollectionParts/{partId}/Sections")
    @Operation(summary = "通过试卷部分编号获取题集试卷节列表")
    @HandleResourceException
    public RestResponse<Collection<ColllectionSectionVO>> getCollectionSections(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                @PathVariable("partId") long partId) {
        return ResourceUtils.getListById(request, response, partId, CollectionPartAppService::getCollectionSections);
    }

    /**
     * 创建题集试卷节
     *
     * @param command
     * @return
     */
    @PostMapping("CollectionParts/{partId}/Sections")
    @Operation(summary = "创建题集试卷节")
    @HandleResourceException
    public RestResponse<ColllectionSectionVO> createCollectionSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                      @RequestBody CreateCollectionSectionCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, CollectionPartAppService::createCollectionSection);
    }

    /**
     * 更新题集试卷节
     *
     * @param command
     * @return
     */
    @PutMapping("CollectionParts/{partId}/Sections")
    @Operation(summary = "更新题集试卷节")
    @HandleResourceException
    public RestResponse<ColllectionSectionVO> updateCollectionSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                      @RequestBody UpdateCollectionSectionCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, CollectionPartAppService::updateCollectionSection);
    }

    /**
     * 通过试卷节编号删除题集试卷节
     *
     * @param sectionId
     * @return
     */
    @DeleteMapping("CollectionParts/{partId}/Sections")
    @Operation(summary = "通过试卷节编号删除题集试卷节")
    @HandleResourceException
    public RestResponse<Boolean> deleteCollectionSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                         @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, CollectionPartAppService::deleteCollectionSection);
    }
}
